//
//  AY_RecommendView.m
//  AiYue
//
//  Created by xyh on 2025/1/19.
//

#import "AY_RecommendView.h"
@interface AY_RecommendView()
@property (nonatomic, strong) UIView * bg_View;
@property (nonatomic, strong) UIImageView * bgTopImgView;
@property (nonatomic, strong) UIImageView * topTagImgView;
@property (nonatomic, strong) UILabel * actTipLabel;
@property (nonatomic, strong) UILabel * classifyLabel;
@property (nonatomic, strong) UIImageView * bookBgImgView;
@property (nonatomic, strong) UILabel * bookNameLabel;
@property (nonatomic, strong) UILabel * authorLabel;
@property (nonatomic, strong) UIButton * readButton;

@end
@implementation AY_RecommendView

- (instancetype)initWithFrame:(CGRect)frame {
    if([super initWithFrame:frame]) {
        [self initWithAllUI];
    }
    return self;
}


- (void)initWithAllUI {
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    self.bg_View = [AYTool createViewWithFrame:CGRectZero andBg:[UIColor clearColor] andRadius:YNUM(16) andAddSubView:self];
    [self.bg_View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self);
        make.height.mas_offset(YNUM(312));
        make.width.mas_offset(YNUM(252));
    }];
    
    self.bgTopImgView = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"icon_recommend_book_bg.png") andBg:[UIColor clearColor] andRadius:0.0 andAddSubView:self.bg_View];
    [self.bgTopImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bg_View);
        make.right.equalTo(self.bg_View);
        make.left.equalTo(self.bg_View);
        make.height.mas_offset(YNUM(312));
    }];
    
    self.topTagImgView = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"icon_today_book_tag.png") andBg:[UIColor clearColor] andRadius:0.0 andAddSubView:self];
    [self.topTagImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bg_View).offset(-YNUM(6));
        make.left.equalTo(self.bg_View).offset(YNUM(29));
        make.width.mas_equalTo(YNUM(29));
        make.height.mas_offset(YNUM(94));
    }];
    self.actTipLabel = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#34220B") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(22)] andLines:1 andRadius:0 andAddSubView:self.bg_View];
    self.actTipLabel.textAlignment = NSTextAlignmentRight;
    [self.actTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bg_View).offset(-YNUM(24));
        make.top.equalTo(self.bg_View).offset(YNUM(27));
    }];
    
    self.classifyLabel = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#C38854") andBgColor:COLOR(@"#FCEFDC") andFont:[UIFont systemFontOfSize:YNUM(11)] andLines:1 andRadius:YNUM(10) andAddSubView:self.bg_View];
    self.classifyLabel.layer.borderColor = COLOR(@"#F9E2C1").CGColor;
    self.classifyLabel.layer.borderWidth = 1;
    [self.classifyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bg_View).offset(-YNUM(24));
        make.top.equalTo(self.actTipLabel.mas_bottom).offset(YNUM(5));
        make.height.mas_equalTo(YNUM(20));
    }];
    
    self.bookBgImgView = [AYTool createImageViewWithFrame:CGRectZero andImage:nil andBg:[UIColor clearColor] andRadius:0.0 andAddSubView:self.bg_View];
    self.bookBgImgView.contentMode = UIViewContentModeScaleAspectFill;
    [self.bookBgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bg_View).offset(YNUM(25));
        make.top.equalTo(self.topTagImgView.mas_bottom).offset(YNUM(28));
        make.width.mas_equalTo(YNUM(88));
        make.height.mas_equalTo(YNUM(122));
    }];
    self.bookNameLabel = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#34220B") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(16)] andLines:2 andRadius:0 andAddSubView:self.bg_View];
    self.bookNameLabel.textAlignment = NSTextAlignmentRight;
    [self.bookNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bg_View).offset(-YNUM(24));
        make.top.equalTo(self.bookBgImgView.mas_top).offset(YNUM(16));
        make.left.equalTo(self.bookBgImgView.mas_right).offset(YNUM(5));
    }];
    self.authorLabel = [AYTool createLabelWithFrame:CGRectZero andText:nil andTextColor:COLOR(@"#34220B") andBgColor:nil andFont:[UIFont systemFontOfSize:YNUM(11)] andLines:1 andRadius:0 andAddSubView:self.bg_View];
    self.authorLabel.textAlignment = NSTextAlignmentRight;
    [self.authorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bg_View).offset(-YNUM(24));
        make.top.equalTo(self.bookNameLabel.mas_bottom).offset(YNUM(15));
    }];
    
    self.readButton = [AYTool createButtonWithFrame:CGRectZero andTitle:@"立即阅读" andFont:[UIFont boldSystemFontOfSize:YNUM(18)] andTitleColor:[UIColor whiteColor] andBg:[AYTool customJianBianColorWithColor0:COLOR(@"#F0987A") withColor1:COLOR(@"#ED7666") withFrame:CGSizeMake(YNUM(204), YNUM(44)) withType:1] andImage:nil andBgImage:nil andTarget:self andAction:@selector(readAction) andRadius:YNUM(22) andAddSubView:self.bg_View];
    [self.readButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bg_View).offset(YNUM(-13));
        make.left.equalTo(self.bg_View).offset(YNUM(24));
        make.right.equalTo(self.bg_View).offset(YNUM(-24));
        make.height.mas_offset(YNUM(44));
    }];
}

- (void)setRecommendModel:(BookRecommendModel *)recommendModel
{
    _recommendModel = recommendModel;
    self.actTipLabel.text = recommendModel.actTitle;
    self.classifyLabel.text = [NSString stringWithFormat:@"   %@   ",recommendModel.articleCategoryName];
    self.bookNameLabel.text = recommendModel.articleTitle;
    self.authorLabel.text = [NSString stringWithFormat:@"%@ 著",recommendModel.articleAuthor];
    [self.bookBgImgView sd_setImageWithURL:[NSURL URLWithString:recommendModel.articleImg]];
}

// 显示View
- (void)showPopView{
    [[[UIApplication sharedApplication] windows].firstObject addSubview:self];
}

// 隐藏View
- (void)dismissPopView{
    if(self) {
        [self removeFromSuperview];
    }
}

- (void)readAction {
    [self dismissPopView];
    if(self.readBlock){
        self.readBlock(self.recommendModel.articleId);
    }
}


@end
